/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.sack.picasawebalbums;

import java.util.Enumeration;
import javax.swing.tree.DefaultMutableTreeNode;
import org.openide.util.Exceptions;

/**
 *
 * @author admin
 */
public class SlideShowThread extends Thread{
    PicasaAlbumTopComponent topComponent;
    DefaultMutableTreeNode currentNode;
    SlideShowThread(PicasaAlbumTopComponent aThis, DefaultMutableTreeNode currentNode) {
        this.topComponent = aThis;
        this.currentNode = currentNode;
    }
    
    
    public void run(){
               if(currentNode!=null){
        Object currentDataNode = currentNode.getUserObject();
            if(currentDataNode instanceof Album){
                playAlbum(currentNode);
            }
            if(currentDataNode instanceof Photo){
                playPhoto(currentNode);
            }
}
              topComponent.Stop.setEnabled(false);
              topComponent.PlaySlideShow.setEnabled(true);
              topComponent.jScrollPane1.setVisible(true);
              topComponent.jSplitPane1.setDividerLocation(120);
              topComponent.ssThread = null;
              
    }
        private void playAlbum(DefaultMutableTreeNode currentNode) {
        DefaultMutableTreeNode photoNode;
        Object photoDataNode;
        if(currentNode.getChildCount()>0){
            Enumeration e = currentNode.children();
            while(e.hasMoreElements()){
                try {
                    photoNode = (DefaultMutableTreeNode) e.nextElement();
                    photoDataNode = photoNode.getUserObject();
                    topComponent.doPhotoProcess(photoDataNode);
                    Thread.sleep(5000);
                } catch (InterruptedException ex) {
                    Exceptions.printStackTrace(ex);
                }
            }
        }
    }
    private void playPhoto(DefaultMutableTreeNode currentNode) {
        DefaultMutableTreeNode photoNode;
        Object photoDataNode;
        int currentphoto = currentNode.getParent().getIndex(currentNode);
        int totalphotos = currentNode.getParent().getChildCount();
        for(int i=currentphoto;i<totalphotos-1;i++){
            try {
                photoNode = currentNode;
                photoDataNode = photoNode.getUserObject();
                topComponent.doPhotoProcess(photoDataNode);
                Thread.sleep(5000);
                currentNode = photoNode.getNextSibling();
            } catch (InterruptedException ex) {
                Exceptions.printStackTrace(ex);
            }
        }
    }
}